package com.ss.android.article.base.feature.search;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;

import com.bytedance.common.utility.Logger;
import com.ss.android.article.base.app.AppData;
import com.ss.android.article.base.feature.app.b.DBHelper;
import com.ss.android.article.browser.R;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public final class u extends BaseAdapter implements Filterable {
    final class a {
        View a;
        TextView b;
        View c;

        a(byte arg1) {
            this();
        }

        private a() {

        }
    }

    public interface b {
        void c(String arg1);

        void d(String arg1);

        void i();

        void k();
    }

    static final class c implements Animator.AnimatorListener {
        private final View a;
        private final f b;
        private final int c;
        private final u d;

        public c(u arg1, View arg2, f arg3, int arg4) {
            this.d = arg1;
            this.a = arg2;
            this.b = arg3;
            this.c = arg4;
        }

        public final void onAnimationCancel(Animator arg1) {
        }

        public final void onAnimationEnd(Animator arg5) {
            this.a.setScaleY(1f);
            this.a.setTranslationX(0f);
            this.a.getLayoutParams().height = this.c;
            --this.d.g;
            if(this.d.g == 0) {
                this.d.c.removeAll(this.d.d);
                this.d.d.clear();
                if(this.d.c.size() == 1 && this.d.c.get(0).a == 1) {
                    this.d.c.remove(0);
                    if(u.a(this.d) != null) {
                        u.a(this.d).k();
                    }
                }

                this.d.notifyDataSetChanged();
            }
        }

        public final void onAnimationRepeat(Animator arg1) {
        }

        public final void onAnimationStart(Animator arg4) {
            this.d.d.add(this.b);
            DBHelper.getInstance(this.d.a).a(this.d.b, this.b.b.toString());
            ++this.d.g;
        }
    }

    static final class d implements ValueAnimator.AnimatorUpdateListener {
        private final View a;

        public d(View arg1) {

            this.a = arg1;
        }

        public final void onAnimationUpdate(ValueAnimator arg5) {
            this.a.setTranslationX(((Float)arg5.getAnimatedValue()).floatValue());
            this.a.setScaleY(1f - arg5.getAnimatedFraction());
            ViewGroup.LayoutParams v0 = this.a.getLayoutParams();
            v0.height = ((int)((((float)v0.height)) * (1f - arg5.getAnimatedFraction())));
            if(this.a.getLayoutParams().height != 0) {
                this.a.requestLayout();
            }
        }
    }

    static final class e extends Filter {
        private final u a;

        e(u arg1, byte arg2) {
            this(arg1);
        }

        private e(u arg1) {
            this.a = arg1;

        }

        protected final Filter.FilterResults performFiltering(CharSequence arg12) {
            List v0_4;
            SpannableString v0_3 = null;
            String v0;
            Filter.FilterResults v3 = new Filter.FilterResults();
            List v1 = null;
            ArrayList v4 = new ArrayList();
            if(Logger.debug()) {
                String v2 = "suggestion";
                StringBuilder v5 = new StringBuilder("prefix = ");
                v0 = arg12 == null ? "null" : arg12.toString();
                Logger.d(v2, v5.append(v0).toString());
            }

            if(arg12 == null || arg12.length() == 0) {
                if(!AppData.getInstance().getAbSettings().isWebSearchEnable()) {
                    v1 = DBHelper.getInstance(this.a.a).getCityList(this.a.b);
                    if(v1.size() > 0) {
                        Iterator v2_2 = v1.iterator();
                        while(v2_2.hasNext()) {
                            ((List)v4).add(new f((String)v2_2.next()));
                        }

                        ((List)v4).add(new f());
                        v0_4 = v1;
                    }else {
                        v0_4 = v1;
                    }
                }else {
                    v0_4 = v1;
                }
            } else {
                String v5_1 = arg12.toString();
                List v2_1 = android.support.design.a.b(v5_1, this.a.b);
                if(v2_1.size() > 0) {
                    Iterator v6 = v2_1.iterator();
                    while(v6.hasNext()) {
                        Object v0_1 = v6.next();
                        int v8 = this.a.f.getColor(com.ss.android.article.browser.R.color.ssxinzi4);
                        if(!android.support.design.a.isTextEmpty(((String)v0_1)) && !android.support.design.a.isTextEmpty(v5_1)) {
                            SpannableString v1_1 = new SpannableString(((CharSequence)v0_1));
                            int v0_2 = ((String)v0_1).indexOf(v5_1);
                            if(v0_2 >= 0) {
                                v1_1.setSpan(new ForegroundColorSpan(v8), v0_2, v5_1.length() + v0_2, 17);
                            }

                            v0_3 = v1_1;
                        }
                        ((List)v4).add(new f(v0_3));
                    }
                }
                v0_4 = v2_1;
            }

        label_45:
            if(Logger.debug()) {
                String v1_2 = "suggestion";
                StringBuilder v2_3 = new StringBuilder("suggestionList = ");
                v0 = v0_4 == null ? "null" : Arrays.toString(v0_4.toArray());
                Logger.d(v1_2, v2_3.append(v0).toString());
            }

            v3.values = v4;
            v3.count = ((List)v4).size();
            return v3;
        }

        protected final void publishResults(CharSequence arg3, Filter.FilterResults arg4) {
            this.a.c.clear();
            if(arg4 != null && arg4.values != null) {
                this.a.c.addAll((List)arg4.values);
            }

            u v1 = this.a;
            boolean v0 = arg3 == null || arg3.length() == 0 ? true : false;
            v1.e = v0;
            if(Logger.debug()) {
                Logger.d("suggestion", "publishResults");
            }

            if(this.a.c.size() > 0) {
                if(Logger.debug()) {
                    Logger.d("suggestion", "notifyDataSetChanged");
                }

                if((this.a.e) && u.a(this.a) != null) {
                    u.a(this.a).d("history_explore");
                }

                this.a.notifyDataSetChanged();
            }
        }
    }

    static final class f {
        int a;
        CharSequence b;

        f(CharSequence arg2) {

            this.a = 0;
            this.b = arg2;
        }

        f() {

            this.a = 1;
        }
    }

    final class g {
        View a;
        ImageView b;
        TextView c;
        ImageView d;
        View e;

        g(byte arg1) {
            this();
        }

        private g() {

        }
    }

    Context a;
    int b;
    final List<u.f> c;
    final List<u.f> d;
    boolean e;
    Resources f;
    int g;
    private LayoutInflater h;
    private e i;
    private b j;
    private View.OnClickListener k;

    public u(Context arg2, int arg3, u.b arg4) {
        this.a = null;
        this.b = 0;
        this.c = new ArrayList();
        this.d = new ArrayList();
        this.e = true;
        this.k = new v(this);
        this.a = arg2;
        this.b = arg3;
        this.j = arg4;
        this.h = LayoutInflater.from(this.a);
        this.f = this.a.getResources();
    }

    static b a(u arg1) {
        return arg1.j;
    }

    public final int getCount() {
        if(Logger.debug()) {
            Logger.d("suggestion", "getCount = " + String.valueOf(this.c.size()));
        }

        return this.c.size();
    }

    public final Filter getFilter() {
        if(this.i == null) {
            this.i = new e(this, (byte) 0);
        }

        return this.i;
    }

    public final Object getItem(int arg2) {
        return null;
    }

    public final long getItemId(int arg3) {
        return ((long)arg3);
    }

    public final int getItemViewType(int arg2) {
        return this.c.get(arg2).a;
    }

    public final View getView(int arg8, View arg9, ViewGroup arg10) {
        g v1_1;
        a v0;
        int v6 = R.color.ssxinxian1;
        int v5 = R.drawable.b_;
        switch(this.getItemViewType(arg8)) {
            case 0: {
                if(arg9 == null) {
                    v1_1 = new g((byte) 0);
                    arg9 = this.h.inflate(com.ss.android.article.browser.R.layout.iq, arg10, false);
                    v1_1.a = arg9.findViewById(com.ss.android.article.browser.R.id.abk);
                    v1_1.b = arg9.findViewById(com.ss.android.article.browser.R.id.abl);
                    v1_1.c = arg9.findViewById(com.ss.android.article.browser.R.id.abm);
                    v1_1.d = arg9.findViewById(com.ss.android.article.browser.R.id.delete);
                    v1_1.e = arg9.findViewById(com.ss.android.article.browser.R.id.divider);
                    v1_1.d.setTag(com.ss.android.article.browser.R.id.b2, arg9);
                    arg9.setTag(v1_1);
                }
                else {
                    v1_1 = (u.g) arg9.getTag();
                }

                if(Logger.debug()) {
                    Logger.d("suggestion", "getView position = " + String.valueOf(arg8) + " suggestion = " + this.c.get(arg8).b);
                }

                v1_1.d.setTag(com.ss.android.article.browser.R.id.b0, this.c.get(arg8));
                v1_1.a.setTag(com.ss.android.article.browser.R.id.b3, this.c.get(arg8).b.toString());
                v1_1.a.setOnClickListener(this.k);
                v1_1.d.setOnClickListener(this.k);
                if(this.e) {
                    v1_1.d.setVisibility(View.VISIBLE);
                    v1_1.b.setImageDrawable(this.f.getDrawable(com.ss.android.article.browser.R.drawable.o7));
                }
                else {
                    v1_1.d.setVisibility(View.GONE);
                    Drawable v0_2 = this.f.getDrawable(com.ss.android.article.browser.R.drawable.detail_search_icon);
                    if(v0_2 != null) {
                        v0_2.setAlpha(64);
                    }

                    v1_1.b.setImageDrawable(v0_2);
                }

                v1_1.c.setText(this.c.get(arg8).b);
                v1_1.a.setBackgroundDrawable(this.f.getDrawable(v5));
                v1_1.c.setTextColor(this.f.getColor(com.ss.android.article.browser.R.color.ssxinzi1));
                v1_1.d.setImageDrawable(this.f.getDrawable(com.ss.android.article.browser.R.drawable.o5));
                v1_1.e.setBackgroundColor(this.f.getColor(v6));
                arg9.requestLayout();
                break;
            }
            case 1: {
                if(arg9 == null) {
                    a v1 = new a((byte) 0);
                    arg9 = this.h.inflate(com.ss.android.article.browser.R.layout.ip, arg10, false);
                    v1.a = arg9.findViewById(com.ss.android.article.browser.R.id.abj);
                    v1.b = arg9.findViewById(com.ss.android.article.browser.R.id.content);
                    v1.c = arg9.findViewById(com.ss.android.article.browser.R.id.bottom_divider);
                    arg9.setTag(v1);
                    v0 = v1;
                }
                else {
                    v0 = (u.a) arg9.getTag();
                }

                v0.a.setOnClickListener(this.k);
                v0.a.setBackgroundDrawable(this.f.getDrawable(v5));
                v0.b.setTextColor(this.f.getColor(com.ss.android.article.browser.R.color.ssxinzi3));
                v0.c.setBackgroundColor(this.f.getColor(v6));
                break;
            }
        }

        return arg9;
    }

    public final int getViewTypeCount() {
        return 2;
    }
}

